<?php 
session_start();
require('conf/runtime.conf.php');
require( DOM_PATH . 'Dom.init.php');

DomSql::init(MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE);
DomAr::init();	

if( $_POST['install'] ) {
	
	try {
		$user = AdminUser::load(1);
		if( $user ) {
			die('Installeerimine ei ole lubatud!');
		}
	} catch( Exception $e ) {}
	
	WbPageBasic::getTable()->update();
	WbPageFeedback::getTable()->update();
	WbPageLink::getTable()->update();
	WbPageNews::getTable()->update();
	WbSite::getTable()->update();
	
	AdminUser::getTable()->update();
	DomUserActionLog::getTable()->update();
	
	echo 'Tabelid loodud<br />';
	
	$user = new AdminUser();
	$user->username = $_POST['username'];
	$user->password = $_POST['password'];
	$user->emailConfirmed = true;
	$user->rights->set('manageAdminUsers', true);
	$user->rights->set('manageSites',true);
	
	$site = new WbSite();
	$site->name = $_POST['siteName'];
	$site->isMainSite = true;
	$site->template = 'Default';
	
	$page = new WbPageBasic();
	$page->name = 'Esileht';
	$page->isFrontPage = true;
	$page->template = 'Default';
	$site->pages[] = $page;
	
	
	if( !$user->save()) {
		echo 'Kasutaja salvestamine ebaõnnestus!';
		debug($user);
	} else {
		echo 'Kasutaja loodud!<br />';
	}
	
	if( !$site->save() ) {
		echo 'Lehe salvestamine ebaõnnestus';
		debug($site);
	} else {
		echo 'Leht loodud<br />';
	}
}
?>
<h1>Web base install</h1>
<form name="" action="" method="post">
	<table>
		<tr>
			<td>Admin kasutaja:</td>
			<td><input type="text" value="" name="username" /></td>
		</tr>
		<tr>
			<td>Parool:</td>
			<td><input type="password" value="" name="password" /></td>
		</tr>
		<tr>
			<td>Lehe nimi</td>
			<td><input type="text" value="" name="siteName" /></td>
		</tr>
		<tr>
			<td></td>
			<td><input type="submit" name="install" value="Installeeri"/></td>
		</tr>
	</table>
</form>